package beautysalonsystem.Util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class MailServiceUtil {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    public void sendBookingNotification(String to, String customerName, String date) {
        MimeMessage message = mailSender.createMimeMessage();

        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject("【预约提醒】新客户已下单");

            String html = "<div style='font-family: Arial, sans-serif; padding: 20px;'>"
                    + "<h2 style='color: #1e90ff;'>💡 您有新的预约！</h2>"
                    + "<p><strong>顾客：</strong> " + customerName + "</p>"
                    + "<p><strong>预约时间：</strong> " + date + "</p>"
                    + "<p>请尽快登录系统查看详情并进行确认处理。</p>"
                    + "<hr style='border: none; border-top: 1px solid #eee;'/>"
                    + "<p style='font-size: 12px; color: gray;'>本邮件由系统自动发送，请勿回复。</p>"
                    + "</div>";

            helper.setText(html, true);
            mailSender.send(message);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
